package com.ruoyi.xysl.controller;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.xysl.api.domain.XyslGarden;
import com.ruoyi.xysl.service.ISysXyslGardenService;
import com.ruoyi.xysl.service.ISysXyslUserService;
import com.xysl.api.domain.XyslUser;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import java.util.Date;
import java.util.List;

/**
 * 用户Controller
 *
 * @author xysl
 * @date 2024-12-27
 */
@RestController
@RequestMapping("/xyslUser")
public class SysXyslUserController extends BaseController
{
    @Autowired
    private ISysXyslUserService xyslUserService;

    @Autowired
    private ISysXyslGardenService xyslGardenService;
    /**
     * 查询用户列表
     */
    @RequiresPermissions("xysl:user:list")
    @GetMapping("/list")
    public TableDataInfo list(XyslUser xyslUser)
    {
        startPage();
        List<XyslUser> list = xyslUserService.selectXyslUserList(xyslUser);
        return getDataTable(list);
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("xysl:user:edit")
    @Log(title = "用户状态修改", businessType = BusinessType.UPDATE)
    @GetMapping("/changeUserStatus")
    public AjaxResult changeUserStatus(XyslUser user)
    {
        user.setUpdatedAt(new Date());
        return toAjax(xyslUserService.updateXyslUser(user));
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("xysl:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XyslUser xyslUser)
    {
        List<XyslUser> list = xyslUserService.selectXyslUserList(xyslUser);
        ExcelUtil<XyslUser> util = new ExcelUtil<XyslUser>(XyslUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @RequiresPermissions("xysl:user:query")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id)
    {

        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id))
        {
            ajax.put(AjaxResult.DATA_TAG, xyslUserService.selectXyslUserById(id));
        }
        List<XyslGarden> gardenList = xyslGardenService.selectXyslGardenList(new XyslGarden());
        ajax.put("gardens", gardenList);
        return ajax;
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("xysl:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XyslUser xyslUser)
    {   xyslUser.setPassword(SecurityUtils.encryptPassword("123456"));
        return toAjax(xyslUserService.insertXyslUser(xyslUser));
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("xysl:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XyslUser xyslUser)
    {
        return toAjax(xyslUserService.updateXyslUser(xyslUser));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("xysl:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xyslUserService.deleteXyslUserByIds(ids));
    }

    /**
     * 重置密码
     */
    @RequiresPermissions("xysl:user:resetPwd")
    @Tag(name = "重置密码", description = "重置密码")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody XyslUser user)
    {
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(xyslUserService.resetPwd(user));
    }

}
